package com.kolaxy.netty.basic;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyClient {

    public static void main(String[] args) throws InterruptedException {
        //1.创建一个线程组
        EventLoopGroup group = new NioEventLoopGroup();

        //2.创建客户端的启动助手，完成相关配置
        Bootstrap bootstrap = new Bootstrap();

        //3设置线程组
        bootstrap.group(group);

        //4.设置客户端的实现类
        bootstrap.channel(NioSocketChannel.class);

        //5.创建通道初始化对象
        bootstrap.handler(new ChannelInitializer<SocketChannel>() {
            protected void initChannel(SocketChannel ch) throws Exception {
                //6. 往pipeline链中添加自定义handler
                ch.pipeline().addLast(new NettyClientHandler());
            }
        });
        System.out.println("client is ready....");

        //7. 启动客户端去连接服务器sycn 表示异步
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 9999).sync();

        //8.关闭连接
//        channelFuture.channel().closeFuture();
//        //9.关闭线程池
//        group.shutdownGracefully();
    }
}
